FROM openeuler-22.03-lts-sp4:latest AS build

ARG VERSION=1.28.0
ARG PCRE2_VERSION=10.45
ARG ZLIB_VERSION=1.3.1
ARG OPENSSL_VERSION=3.5.0

COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY patch/gitmodules_patch.diff /gitmodules_patch.diff
COPY patch/nginxconf_patch.diff /nginxconf_patch.diff

RUN set -eux; \
    dnf install -y gcc make git && dnf clean all

RUN set -eux; \
    git clone -b release-${VERSION} https://gitcode.com/gh_mirrors/ng/nginx.git nginx-src && \
    git clone -b v${ZLIB_VERSION} https://gitcode.com/gh_mirrors/zl/zlib.git zlib-src && \
    git clone -b pcre2-${PCRE2_VERSION} https://gitcode.com/gh_mirrors/pc/pcre2.git pcre2-src && \
    git clone -b openssl-${OPENSSL_VERSION} https://gitcode.com/gh_mirrors/ope/openssl.git openssl-src && \
    cd pcre2-src && \
    git apply /gitmodules_patch.diff && \
    git submodule update --init && \
    cd ../nginx-src/conf && \
    git apply /nginxconf_patch.diff && \
    cd ../ && \
    ./auto/configure \
        --prefix=/usr/local/nginx \
        --sbin-path=/usr/local/bin/nginx \
        --user=nginx \
        --group=nginx \
        --build="build by Air China IT Group." \
        --with-threads \
        --with-file-aio \
        --with-http_ssl_module \
        --with-http_v2_module \
        --with-http_v3_module \
        --with-http_realip_module \
        --with-http_addition_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_auth_request_module \
        --with-http_random_index_module \
        --with-http_secure_link_module \
        --with-http_degradation_module \
        --with-http_slice_module \
        --with-http_stub_status_module \
        --with-stream \
        --with-stream_ssl_module \
        --with-stream_realip_module \
        --with-stream_ssl_preread_module \
        --with-pcre=../pcre2-src \
        --with-pcre-jit \
        --with-zlib=../zlib-src \
        --with-openssl=../openssl-src && \
    make -j$(nproc) && \
    make install && \
    cd /usr/local/nginx && \
    tar zcf /ngx-template.tar.gz *

FROM openeuler-22.03-lts-sp4:latest

COPY gosu-arm64 /usr/local/bin/gosu
COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY --from=build /usr/local/bin/nginx /usr/local/bin/nginx
COPY --from=build /ngx-template.tar.gz /opt/

ENV GOSU_VERSION 1.17
RUN set -eux; \
    chmod +x /usr/local/bin/gosu

RUN set -eux; \
    dnf install -y shadow ca-certificates binutils glibc-all-langpacks && \
    groupadd -g 70 nginx; \
    useradd -r -u 70 -g nginx -m -d /home/nginx nginx; \
    mkdir -p /home/nginx/instance && \
    chown -R nginx:nginx /home/nginx/instance && \
    dnf clean all

ENV LANG en_US.UTF-8
EXPOSE 8080 8443
VOLUME ["/home/nginx/instance"]

COPY entrypoint.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/entrypoint.sh; \
    ln -s /usr/local/bin/entrypoint.sh / # backwards compat

ENTRYPOINT ["entrypoint.sh"]
CMD ["nginx", "-p", "/home/nginx/instance", "-g", "daemon off;"]
